﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Datorgrafik_lab1.Models.Airplane;
using Datorgrafik_lab1.Helpers;
using Datorgrafik_lab1.Components.Input;

namespace Datorgrafik_lab1.Objects.Airplane{
    public class Airplane : GameObject
    {
        public Vector3 CameraPosition { get; set; }

        private float TurnSpeed = 1f;
        private float MoveSpeed = 0.005f;

        private AirplaneModel model;

        public Airplane(GraphicsDevice graphics, Vector3 pos, float scale)
            : base(graphics, pos, Quaternion.Identity, scale)
        {
            AddChild(new Propeller(this.device, new Vector3(2.5f, 0.4f, -0.25f), 0.5f));
            AddChild(new Propeller(this.device, new Vector3(-2.5f, 0.4f, -0.25f), 0.5f));            
        }

        protected override void InitializeModel() {
            model = new AirplaneModel();

            this.vertices = model.vertices;
            this.indices = model.indices;
        }

        public void Update(float elapsedTime) {
            float distance = (float)(this.MoveSpeed * elapsedTime);

            Vector3 addVector = Vector3.Transform(new Vector3(0, 0, 1), this.Rotation);
            this.Position += addVector * distance;
            this.CameraPosition += addVector * distance;

            base.Update();
        }

        public void PerformAction(ActionType action, float elapsedTime) {
            float turningSpeed = elapsedTime / 1000;
            float airplaneTurnSpeed = this.TurnSpeed;
            float airplaneMoveSpeed = this.MoveSpeed;
            turningSpeed *= airplaneTurnSpeed;

            float leftRightRot = 0;
            float upDownRot = 0;
            float yawRot = 0;

            switch(action) {
                case ActionType.Accelerate:
                    if(airplaneTurnSpeed > 0.6)
                        airplaneTurnSpeed -= 0.1f;
                    if(airplaneMoveSpeed < 0.05)
                        airplaneMoveSpeed += 0.001f;
                    break;
                case ActionType.Decelerate:
                    if(airplaneTurnSpeed < 1.4)
                        airplaneTurnSpeed += 0.1f;
                    if(airplaneMoveSpeed > 0.005)
                        airplaneMoveSpeed -= 0.001f;
                    break;
                case ActionType.PitchUp:
                    upDownRot -= turningSpeed;
                    break;
                case ActionType.PitchDown:
                    upDownRot += turningSpeed;
                    break;
                case ActionType.RollLeft:
                    leftRightRot += turningSpeed;
                    break;
                case ActionType.RollRight:
                    leftRightRot -= turningSpeed;
                    break;
                case ActionType.YawLeft:
                    yawRot += turningSpeed;
                    break;
                case ActionType.YawRight:
                    yawRot -= turningSpeed;
                    break;
            }

            Quaternion additionalRot = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), leftRightRot)
                * Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRot)
                * Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), yawRot);

            this.Rotation *= additionalRot;
            this.TurnSpeed = airplaneTurnSpeed;
            this.MoveSpeed = airplaneMoveSpeed;
        }
    }
}
